package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxUserOpinionsAddBo;
import com.ruoyi.business.bo.HcxUserOpinionsQueryBo;
import com.ruoyi.business.bo.HcxUserOpinionsEditBo;
import com.ruoyi.business.domain.HcxUserOpinions;
import com.ruoyi.business.mapper.HcxUserOpinionsMapper;
import com.ruoyi.business.vo.HcxUserOpinionsVo;
import com.ruoyi.business.service.IHcxUserOpinionsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户意见反馈Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Service
public class HcxUserOpinionsServiceImpl extends ServicePlusImpl<HcxUserOpinionsMapper, HcxUserOpinions> implements IHcxUserOpinionsService {

    @Override
    public HcxUserOpinionsVo queryById(Long id){
        return getVoById(id, HcxUserOpinionsVo.class);
    }

    @Override
    public TableDataInfo<HcxUserOpinionsVo> queryPageList(HcxUserOpinionsQueryBo bo) {
        PagePlus<HcxUserOpinions, HcxUserOpinionsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxUserOpinionsVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxUserOpinionsVo> queryList(HcxUserOpinionsQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxUserOpinionsVo.class);
    }

    private LambdaQueryWrapper<HcxUserOpinions> buildQueryWrapper(HcxUserOpinionsQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxUserOpinions> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HcxUserOpinions::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), HcxUserOpinions::getUserName, bo.getUserName());
        lqw.like(StrUtil.isNotBlank(bo.getRealName()), HcxUserOpinions::getRealName, bo.getRealName());
        lqw.like(StrUtil.isNotBlank(bo.getPhonenumber()), HcxUserOpinions::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StrUtil.isNotBlank(bo.getDescription()), HcxUserOpinions::getDescription, bo.getDescription());
        lqw.eq(StrUtil.isNotBlank(bo.getContactInfo()), HcxUserOpinions::getContactInfo, bo.getContactInfo());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxUserOpinions::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxUserOpinionsAddBo bo) {
        HcxUserOpinions add = BeanUtil.toBean(bo, HcxUserOpinions.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxUserOpinionsEditBo bo) {
        HcxUserOpinions update = BeanUtil.toBean(bo, HcxUserOpinions.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxUserOpinions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxUserOpinions(List<HcxUserOpinionsVo> hcxUserOpinionsList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxUserOpinionsList), "导入数据不能为空");
        int successNum = 0;
        for (HcxUserOpinionsVo hcxUserOpinions : hcxUserOpinionsList) {
            HcxUserOpinions add = BeanUtil.toBean(hcxUserOpinions, HcxUserOpinions.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxUserOpinionsList.size(), successNum);
        if (successNum != hcxUserOpinionsList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxUserOpinionsList.size() - successNum);
        }
        return msg;
    }
}
